﻿ 
#include "stdafx.h"
#include "date.h"
 
int __MAX (int a, int b)
{
	if (a>b) return(a) ; else return (b);
}

int __MIN (int a, int b)
{
	if (a>b) return(b); else return (a);
}

//#error

// constructor definition
Date::Date ()
{
	month = day = year = 1;
}

Date::Date (int mn, int dy, int yr)
{
	static int length[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	month = __MAX(1, mn);
	month = __MIN(month,12);

	day = __MAX(1,dy);
	day = __MIN(day, length[month]);

	year = __MAX(1, yr);
}

void Date::display()
{
	static char *name[] = {"nothing", "January", "February", "March", "April",
		"May", "June", "July", "August", "September", "October",
		"November", "December" };

	std::cout << '\n' << name[month] << ' ' << day << "," << year << '\n';
	std::cout << "Days so far: " << DaysSoFar() << '\n';
}

Date::~Date()
{
	std::cout << "Thank You for using DateLine Services and have a nice date\n";
}

bool Date::operator < (const Date& a) const
{
	if( (year == a.year) && (month == a.month) && (day == a.day) )	return false;

	if( year < a.year ) return true;
	if( year == a.year )
	{
		if( month < a.month ) return true;
		if(month == a.month)
		{
			if(day < a.day) return true;
		}
	}

	return false;
}

void Date::operator = (const std::string& a)throw (std::runtime_error)
{
	std::string temp = a;
	int scanres = sscanf(temp.c_str(), "%i%i%i", &day, &month, &year );
	if(scanres != 3)
	{
		throw std::runtime_error("Error read date from string");
	}
}

Date::operator std::string() const
{
	char temp[64];
	sprintf(temp, "%i-%i-%i", day, month, year );
	return std::string(temp);
}

int Date::DaysSoFar()
{
	int total = 0;
	static int length[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	for (int i=1; i < month; i++) total += length[i];
	total += day;
	return (total);
}

int Date::GetMonth()
{
	return month;
}

void Date::SetMonth(int mn)
{
	month = __MAX(1, mn);
	month = __MIN(month, 12);
}





